package com.example.dell.zyfypt_108zct.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dell.zyfypt_108zct.R;
import com.example.dell.zyfypt_108zct.activity.ViewArticleActivity;
import com.example.dell.zyfypt_108zct.activity.ViewCaseActivity;
import com.example.dell.zyfypt_108zct.activity.ViewProjectActivity;
import com.example.dell.zyfypt_108zct.activity.ViewTwareActivity;
import com.example.dell.zyfypt_108zct.activity.ViewVideoActivity;
import com.example.dell.zyfypt_108zct.bean.Bean;
import com.example.dell.zyfypt_108zct.bean.SessionIDBean;
import com.squareup.picasso.Picasso;

import java.util.List;


public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {

    private Context context;//上下文
    private LayoutInflater layoutInflater;//动态加载布局
    private SessionIDBean sessionIDBean;
    private List<Bean> list;
    //自定义构造方法，传递上下文
    public Adapter(Context context) {
        this.context=context;
        layoutInflater= LayoutInflater.from(this.context);
    }
    //自定义：传递数据源
    public void setList(List<Bean> list){
        this.list=list;
    }
    //自定义：ViewHolder子类
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView tvtitle,tvdescript,tvtime;
        Button button;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView=(ImageView) itemView.findViewById(R.id.imageView);
            tvtitle=(TextView) itemView.findViewById(R.id.textView15);
            tvdescript=(TextView) itemView.findViewById(R.id.textView16);
            tvtime=(TextView) itemView.findViewById(R.id.textView17);
        }
    }
    //为了每个Item生成对应的View
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
        return new ViewHolder(v);
    }
    //为ViewHolder中的控件填充值，设置监听
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        Bean articleBean=list.get(position);//去除List一条数据
        if(articleBean==null)
            return;
        //给条目中的空间设置值
        //holder.imageView.setImageResource(articleBean.getImgid());
        holder.tvtitle.setText(articleBean.getName());
        holder.tvdescript.setText(articleBean.getDescription());
        holder.tvtime.setText(articleBean.getUpdate_time());

        Picasso.with(context).load("http://amicool.neusoft.edu.cn/Uploads/"+articleBean.getThumb())
                .placeholder(R.mipmap.ic_launcher)
                .into(holder.imageView);
        //条目整体的点击事件
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取当前条目id
                int id=list.get(position).getId();
                if(sessionIDBean.allpage.equals("文章")){
                    Intent intent=new Intent(context,ViewArticleActivity.class);
                    intent.putExtra("resid",id);
                    context.startActivity(intent);
                }else if(sessionIDBean.allpage.equals("案例")){
                    Intent intent=new Intent(context,ViewCaseActivity.class);
                    intent.putExtra("resid",id);
                    context.startActivity(intent);
                }else if(sessionIDBean.allpage.equals("项目")){
                    Intent intent=new Intent(context,ViewProjectActivity.class);
                    intent.putExtra("resid",id);
                    context.startActivity(intent);
                }else if(sessionIDBean.allpage.equals("课件")){
                    Intent intent=new Intent(context, ViewTwareActivity.class);
                    intent.putExtra("pdfattach",list.get(position).getPdfattach());//
                    intent.putExtra("name",id);
                    context.startActivity(intent);
                }else if(sessionIDBean.allpage.equals("视频")){
                    Intent intent=new Intent(context,ViewVideoActivity.class);
                    intent.putExtra("videopath",list.get(position).getVideopath());
                    context.startActivity(intent);
                }
            }
        });
    }
    //共有多少条目
    @Override
    public int getItemCount() {
        if(list==null) return 0;
        else return list.size();
    }
}
